package com.example.volleyball.playcharting.android;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.*;

import sharedClasses.*;

import android.app.Activity;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class Client 
{
		private Socket connection = null;
		private ObjectOutputStream outStream;
		private HackedObjectInputStream inStream;
		String message;

  
		public Client()
		{
			//System.out.println("here? ");
		}
		
		public void start()
		{
			System.out.println("start");
		
			try
			{

				
				//1. Create the socket
				connection = new Socket("128.180.120.163", 48477);
				//connection = new Socket("localhost", 48477);
				System.out.println("Connected? ");
				System.out.println("Connected to " + connection.getInetAddress());
				
				//3. Initialize streams
				outStream = new ObjectOutputStream(connection.getOutputStream());
				inStream = new HackedObjectInputStream(connection.getInputStream());
				
			}
			catch (Exception e)
			{
				System.out.println(e);
				//logger.finest(e.getStackTrace().toString());
			}
		}
		
		public void sendMessage(String message)
		{
			try
			{
				outStream.writeObject(message);
				outStream.flush();
				//logger.finest("Server: " + message);
			}
			catch (IOException e)
			{
				//logger.finest(e.getStackTrace().toString());
			}
		}
		
		public Play[] getQuickAnalysis()
		{
			try
			{
				outStream.writeObject("timeout");
				outStream.flush();
				Play[] plays = (Play[]) inStream.readObject();
				String temp = (String) inStream.readObject();
				return plays;
			}
			catch (OptionalDataException e)
			{
				// TODO Auto-generated catch block
				//System.err.println(e);
			}
			catch (ClassNotFoundException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
		
		public Play[][] getSeasonalAnalysis()
		{
			try
			{
				outStream.writeObject("analyze");
				outStream.flush();
				Play[][] plays = (Play[][]) inStream.readObject();
				String temp = (String) inStream.readObject();
				return plays;
			}
			catch (OptionalDataException e)
			{
				// TODO Auto-generated catch block
				//System.err.println(e);
			}
			catch (ClassNotFoundException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
		
		public int sendMatch(Match match)
		{
			
			try
			{
				outStream.writeObject(match);
				outStream.writeObject("done");
				outStream.flush();
				int temp;
				temp = (Integer) inStream.readObject();
				if (temp == match.getMatchID())
				{
					return 1;
				}
				else
				{
					sendMessage("Error");
					message = "Error";
					return -1;
				}
			}
			catch (OptionalDataException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (ClassNotFoundException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return -1;

		}
		
		public int sendPlay(Play play)
		{
			
			try
			{
				outStream.writeObject(play);
				outStream.writeObject("done");
				outStream.flush();
				int temp;
				temp = inStream.readInt();
				if (temp == play.getPlayID())
				{
					return 1;
				}
				else
				{
					sendMessage("Error");
					message = "Error";
					return -1;
				}
			}
			catch (OptionalDataException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return -1;
		}
		
		public void stop()
		{
			try
			{
				sendMessage("goodbye");
				message = (String) inStream.readObject();
				//logger.finest("Closing connection");
				outStream.close();
				inStream.close();
				connection.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
				//logger.finest(e.getStackTrace().toString());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

//		@Override
//		protected void onHandleIntent(Intent intent) {
//			// TODO Auto-generated method stub
//			//start();
//			sendMessage("hi");
//		}

	}

